// Created by Simon Pamiés

package SketchPackage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Vector;

import processing.core.PApplet;
import processing.core.PFont;

public class AsciiArt extends PApplet
{

  // which dimensions should have the screen
  int w = screen.width - 200;
  int h = screen.height - 200;

  int x, y;

  public PFont font;

  Cell[][] rect;
  Vector<String> lines;

  int xAxis, yAxis;

  public void init(String filename) throws Exception
  {
    x = y = 0;
    font = createFont( "Verdana", 15, true );
    rect = this.readFile( new File( filename ) );
  }

  private Cell[][] readFile(File file) throws FileNotFoundException, IOException
  {
    FileReader reader = new FileReader( file );
    Scanner scanner = new Scanner( reader );

    int maxwidth = 0;
    lines = new Vector<String>();

    while( scanner.hasNext() )
    {
      String line = scanner.nextLine();
      lines.add( line );

      if( line.length() > maxwidth )
        maxwidth = line.length();
    }

    scanner.close();
    reader.close();

    return lines2Matrix( lines, maxwidth );

  }

  private Cell[][] lines2Matrix(Vector<String> lines1, int maxwidth)
  {

    // which dimensions should have the elements
    int mWidth = 10;
    int mHeight = mWidth;

    // calc how much elements pass into the screen
    xAxis = maxwidth;
    yAxis = lines1.size();

    rect = new Cell[ yAxis ][ xAxis ];

    for( int i = 0; i < yAxis; i++ )
    {
      for( int j = 0; j < xAxis; j++ )
      {
        rect[i][j] = new Cell( this, font );

        // Overwrite dimensions in Matrix.class
        rect[i][j].mWidth = mWidth;
        rect[i][j].mHeight = mHeight;

        if( j != 0 )
        {
          rect[i][j].mPosX += rect[i][j - 1].mPosX += rect[i][j - 1].mWidth;
        } else
        {
          rect[i][j].mPosX = 0;
        }

        if( i != 0 )
        {
          rect[i][j].mPosY += rect[i - 1][j].mPosY += rect[i - 1][j].mHeight;
        } else
        {
          rect[i][j].mPosY = 0;
        }

        System.out.println( i + " " + j + " " + rect[i][j].mPosX + " "
            + rect[i][j].mPosY );

        rect[i][j].mDraw(
            255,
            255,
            255,
            rect[i][j].mPosX,
            rect[i][j].mPosY,
            rect[i][j].mWidth,
            rect[i][j].mHeight );

      }
    }

    return rect;
  }

  public void setup()
  {
    size( w, h );
    frame.setResizable( false );
    rectMode( CORNER );
    frameRate( 30 );
    smooth();
    noStroke();
    background( 0 );

    try
    {
      init( "/Users/artemis/Desktop/ascii.txt" );
    } catch( Exception e )
    {
      e.printStackTrace();
    }
  }

  public void draw()
  {
    if( x >= xAxis - 1 )
    {
      x = 0;
      y += 1;
    } else
    {
      x += 1;
    }

    if( y < yAxis )
    {
      char ch = ' ';
      try
      {
        ch = lines.get( y ).charAt( x );
      } catch( Exception ex )
      {
      }

      System.out.println( "::" + ch + ":: " + y + " " + x );
      this.rect[y][x].cDraw( ch, rect[y][x].mPosX, rect[y][x].mPosY );

    }

  }

  public static void main(String _args[])
  {
    PApplet.main( new String[]
    { SketchPackage.AsciiArt.class.getName() } );
  }
}
